package com.gears_software.sxdc.dccx.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.adapter.HistoryAdatpter;
import com.gears_software.sxdc.dccx.data.adapter.HotCitiesAdapter;

import com.gears_software.sxdc.dccx.data.finder.ICPOrderFinder;
import com.gears_software.sxdc.dccx.data.finder.util.GearsErrorHandler;
import com.gears_software.sxdc.dccx.data.model.Cities;
import com.gears_software.sxdc.dccx.data.model.LineInfoModel;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.ui.util.MaterialDialogProgress;
import com.jaeger.library.StatusBarUtil;


import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

import java.util.ArrayList;
import java.util.List;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by SunMuran on 2017/7/22.
 * 选择城市页面
 */
@EActivity(R.layout.selector_cities)
public class SelectorCitiesActivity extends Activity {
    @App
    DCCXApplication app;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @ViewById
    TextView tvImage;
    @ViewById
    GridView gv_history;
    @ViewById
    GridView gv_hot_cities;
    @ViewById
    EditText clear_eidt;

    @RestService
    ICPOrderFinder icpOrderFinder;
    @Bean
    GearsErrorHandler errorHandler;

    Context context;

    HistoryAdatpter historyAdapter;
    HotCitiesAdapter hotCitiesAdapter;
    LineInfoModel lineInfoModel;
    List<LineInfoModel> historyData = new ArrayList<LineInfoModel>();
    List<LineInfoModel> hotData = new ArrayList<LineInfoModel>();
    MaterialDialogProgress progress;


    @AfterViews
    void created() {
        context = this;
        actionbar_title.setText("选择城市");
        StatusBarCompat.setStatusBarColor(this,ContextCompat.getColor(this, R.color.colorActionbar));

        icpOrderFinder.setRestErrorHandler(errorHandler);
        initIconfont();
        progress = new MaterialDialogProgress(this);
        progress.show("正在加载...");
        getHotData();
        //避免自动弹出软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        clear_eidt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                filterData(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    void initIconfont() {
        back_icon.setTypeface(app.iconfont);
        back_icon.setText(R.string.back_icon);
        right_icon.setVisibility(View.GONE);
        tvImage.setTypeface(app.iconfont);

    }

    @Background
    void getHotData() {
        ResultModel<List<LineInfoModel>> result = icpOrderFinder.getLineInfoList();
        showHotResult(result);

    }

    @UiThread
    void showHotResult(ResultModel<List<LineInfoModel>> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                if (res.getData() != null) {
                    hotData = res.getData();
                    historyData = res.getData();
                    if (hotData != null && hotData.size() > 0) {
                        hotCitiesAdapter = new HotCitiesAdapter(this, hotData);
                        gv_hot_cities.setAdapter(hotCitiesAdapter);

                        historyAdapter = new HistoryAdatpter(SelectorCitiesActivity.this, historyData);
                        gv_history.setAdapter(historyAdapter);
                    }
                }

            }
        }
        progress.dismiss();
    }

    @ItemClick
    void gv_history(LineInfoModel model) {
        Intent intent = new Intent();
        intent.putExtra("cities", model);
        setResult(1, intent);
        finish();
    }

    @ItemClick
    void gv_hot_cities(LineInfoModel model) {
        Intent intent = new Intent();
        intent.putExtra("cities", model);
        setResult(1, intent);
        finish();
    }


    private void filterData(String filterStr) {
        List<LineInfoModel> list = new ArrayList<LineInfoModel>();
        if (TextUtils.isEmpty(filterStr)) {
            getHotData();
        } else {
            list.clear();
            if (hotData != null && hotData.size() > 0) {
                for (LineInfoModel model : hotData) {
                    String name = model.getStartpoint() + model.getEndpoint();
                    if (name.indexOf(filterStr.toString()) != -1) {
                        list.add(model);
                    }
                }
            }
            hotData.clear();
            hotData = list;

        }
        hotCitiesAdapter.updateHotCites(hotData);

    }

    @Click
    void back_icon() {
        finish();
    }


}

